package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ToastUtils;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityWifiSignalBinding;
import flc.ast.utils.NetworkActivitySimulator;
import flc.ast.utils.NetworkSpeedMonitor;
import flc.ast.utils.SignalStrengthMonitor;
import flc.ast.utils.WifiSignalMonitor;

public class WifiSignalActivity extends BaseAc<ActivityWifiSignalBinding> {

    private NetworkSpeedMonitor speedMonitor;

    @Override
    protected int onCreate() {
        return R.layout.activity_wifi_signal;
    }

    @Override
    protected void initView() {
        mDataBinding.ivWifiSignalBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvRetest.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvRetest://重新检测
                if (NetworkUtils.isWifiConnected()) {
                    getData();
                } else {
                    ToastUtils.showShort(getString(R.string.not_connect_wifi));
                }
                break;
        }
    }

    @Override
    protected void initData() {
        if (NetworkUtils.isWifiConnected()) {
            mDataBinding.tvWifiName.setText(getString(R.string.current_network_text) + NetworkUtils.getSSID());
            getData();
        } else {
            mDataBinding.tvWifiName.setText(getString(R.string.no_connect));
        }
    }

    private void getData() {
        // 启动网速监控
        speedMonitor = new NetworkSpeedMonitor((downloadSpeed, uploadSpeed) -> {
            mDataBinding.tvSpeed.setText(String.format("%.1f", downloadSpeed));
        });
        NetworkActivitySimulator simulator = new NetworkActivitySimulator();
        simulator.startSimulation();
        speedMonitor.start();
        showDialog(getString(R.string.loading));
        WifiSignalMonitor wifiMonitor = new WifiSignalMonitor(mContext);
        int rssi = wifiMonitor.getWifiSignalStrength(); // 获取 RSSI
        int signalLevel = wifiMonitor.getSignalLevel(rssi); // 转换为信号等级
        mDataBinding.tvSignalValue.setText(100 + rssi + "");
        switch (signalLevel) {
            case 0:
                mDataBinding.tvNetworkSignal.setText(getString(R.string.signal_name3));
                break;
            case 1:
                mDataBinding.tvNetworkSignal.setText(getString(R.string.signal_name2));
                break;
            case 2:
                mDataBinding.tvNetworkSignal.setText(getString(R.string.signal_name1));
                break;
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                speedMonitor.stop();
                simulator.stopSimulation();
            }
        }, 1000);
    }

}